package cn.com.dcsgo.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * 全局跨域配置，只在prod环境生效
 *
 * @author Dcsgo
 * @since 2025/6/10 下午9:58
 */

@Configuration
@Profile("prod")
public class WebCorsConfig implements WebMvcConfigurer {
    @Value("${origin-host}")
    private String originHost;

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        // 允许所有路径
        registry.addMapping("/**")
                // 允许所有域名，生产建议指定域名
                .allowedOrigins(originHost)
                .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
                .allowedHeaders("*")
                // 允许携带cookie
                .allowCredentials(true)
                // 预检请求缓存时间（秒）
                .maxAge(3600);
    }
}
